package svmc.stp.hoaitt;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class RollingBall {
	static final int N = 100;
	static final int map[][] = new int[N][N];
	static final int parent[][] = new int[N+2][N+2];
	static int answers, n;
	
	public static void main(String[] args) throws FileNotFoundException {
		int T;//number of test case
		
		System.setIn(new FileInputStream("res\\sample_input.txt"));
		
		Scanner sc = new Scanner(System.in);
		T = sc.nextInt();
		
		for (int test_case = 0; test_case < T; test_case++) {
			n = sc.nextInt();
			
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					map[i][j] = sc.nextInt();
				}
			}
			
			//Find min in the matrix
			int min = map[0][0];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					if (map[i][j] < min) {
						min = map[i][j];
					}
				}
			}
			
			//Add map to parent
			for (int i = 0; i < n+2; i++) {
				if ((i == 0) || i == (n+1)) {
					for (int j = 0; j < n+2; j++) {
						parent[i][j] = 501;
						
					}
				} else {
					for (int j = 0; j < n+2; j++) {
						if ((j==0 ) || (j == (n+1))) {
							parent[i][j] = 501;
						} else {
							parent[i][j] = map[i][j];
						}
						
					}
				}
				
			}
			
			//Find max 
			answers = 0;
			for (int i = 0; i < n+2; i++) {
				for (int j = 0; j < n+2; j++) {
					int start = map[i][j];
					int count = 0;
					int tmpMx = 0, tmx1 = 0, tmx2 = 0;
					
					tmx1 = map[i][j-1] > map[i][j+1] ? map[i][j+1] : map[i][j-1];
					tmx2 = map[i-1][j] > map[i+1][j] ? map[i+1][j] : map[i-1][j];
					tmpMx = tmx1 > tmx2 ? tmx2 : tmx1;
					
					while (start > tmpMx) {
						start = tmpMx;
						count++;
					}
					
					if (count > answers) {
						answers = count;
					}
				}
				
			}
			System.out.println("#"+test_case+" "+answers);
			
		}
	}

}
